/*
Hermes Core: General Purpose Mobile Agent Platform
Copyright (C) 2006 UNICAM - Università degli Studi di Camerino

Group leaders: Flavio Corradini and Emanuela Merelli

Developers: Ezio Bartocci, Diego Bonura, Rosario Culmone,
Davide Lorenzoli, Leonardo Mariani, Francesca Piersigilli, 
Lorenzo Scortichini, Leonardo Vito.

GNU Lesser Public License

This library is free software; you can redistribute it 
and/or modify it under the terms of the GNU Lesser General 
Public License as published by the Free Software Foundation, 
version 2.1 of the License.

This library is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public 
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

Emanuela Merelli
Dipartimento di Matematica ed Informatica
Università di Camerino
via Madonna delle Carceri, 62032 Camerino, Italy
E.Mail: emanuela.merelli@unicam.it
*/
package hermesV2.util;

import hermesV2.util.HermesLogger;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
 * It is a container of an object. The object is saved in a byteArray so the
 * receiver, speccially the classLoader, can manages  the object well and asks
 * for new class file dinamically.
 *
 * @author Diego Bonura
 * @version $Revision: 1.1 $
 */
public class SmartPack<T extends Object>  implements java.io.Serializable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 2999890906178097411L;

	/** The className of teh object contained. */
    private String className; // nome della classe

    /** The byteArray of the object contained. */
    private byte[] objBytes; // istanza dell'agente

    /**
     * The costructor of the SmartPack class.
     *
     * @param obj The object to be contained into the SmartPack object.
     */
    public SmartPack(T obj) {
        if (obj == null) {
            className = null;
        } else {
            className = obj.getClass().getName();
        }

        try {
            ByteArrayOutputStream byteOStream = new ByteArrayOutputStream();
            ObjectOutputStream objOStream = new ObjectOutputStream(byteOStream);
            objOStream.writeObject(obj);
            objBytes = byteOStream.toByteArray();
        } catch (IOException e) {
            HermesLogger.log(HermesLogger.WARNING, "IOException", e);
        }
    }

    /**
     * Return the className of the object.
     *
     * @return the className.
     */
    public String getClassName() {
        return className;
    }

    /**
     * Returns the byteArray of the object contained.
     *
     * @return the byteArray of the object.
     */
    public byte[] getObjectByte() {
        return objBytes;
    }

    /**
     * Returns the object contained. It use streams to convert the byteArray to
     * an object.
     *
     * @return the object contained.
     * @throws ClassNotFoundException The class of the object contained didn't found.
     * @throws IOException Problems with the virtual stream.
     */
    public Object getObject() throws ClassNotFoundException, IOException {
        Object o = null;

        //	Qui dovrei deserializzare l'oggetto nel caso sia sicuro di avere il class
        //altrimenti dovrei prendere il pack e deserializzarlo attraverso lo SmartCLassLoader
        ByteArrayInputStream byteIStream = new ByteArrayInputStream(objBytes);
        ObjectInputStream objIStream = new ObjectInputStream(byteIStream);
        o = objIStream.readObject();

        // questo provocher�il caricamento della classe dell'agente
        return o;
    }
}
